package com.haois.mp.database.module.offaccounts.controller;

import com.google.gson.Gson;
import com.haois.mp.database.exception.OutException;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.haois.mp.database.module.remote.service.IYellowService;
import com.haois.mp.database.util.mp.DataCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/5.
 */
@Controller
@RequestMapping("/wxmp/yellow")
public class YellowController {

    private static final Logger logger = LoggerFactory
            .getLogger(GroupController.class);

    @Autowired
    private IYellowService iYellowService;
    Gson gson = new Gson();


    @RequestMapping(value = "/pages")
    public ModelAndView yellowPages(String oid, HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/yellow/yellowPage");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("oid", oid);
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/toInfo")
    public ModelAndView overtGroup(HttpServletRequest request, String oid) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/info/group");
        mv.addObject("oid", oid);
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/toGroup")
    public ModelAndView toGroup(HttpServletRequest request,String memberNum) throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/group/followList");
        mv.addObject("memberNum",memberNum);
        mv.addObject("fromPage", "yellowPage");
        return mv;
    }
    @RequestMapping(value = "/toFollowList")
    public ModelAndView followList(HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/yellow/followList");
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/group")
    public ModelAndView mobile(HttpServletRequest request) throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/group/group");
        String access_token = (String) request.getAttribute("access_token");
        Map result = iYellowService.getFollow(access_token);
        DataCheck.codeNot0X(result);
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("result", gson.toJson(result));
        mv.addObject("fromPage", "yellowPage");
        return mv;
    }


}
